package io.renren.modules.ledger.service.impl;

import io.renren.common.utils.R;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.ledger.dao.MemberDao;
import io.renren.modules.ledger.entity.MemberEntity;
import io.renren.modules.ledger.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity>
        implements MemberService {

    @Autowired
    private MemberDao dao;

    /**
     * 单表分页查询
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<MemberEntity> page = this.selectPage(
                new Query<MemberEntity>(params).getPage(),
                new EntityWrapper<MemberEntity>()
        );
        return new PageUtils(page);
    }

    /**
     * 分页查询
     */
    @Override
    public PageUtils list4Page(Map<String, Object> params){
        Page<MemberEntity> page = new Query<MemberEntity>(params).getPage();
        return new PageUtils(page.setRecords(dao.list4Page(page, params)));
    }

    /**
     * 条件查询
     */
    @Override
    public List<MemberEntity> listByCondition(Map<String, Object> params) {
        return dao.listByCondition(params);
    }

    /**
     * 逻辑删除多条记录
     */
    @Override
    public Boolean deleteLogic(List ids) {
        return dao.deleteLogic(ids);
    }

    /**
     * 根据id查询详情
     */
    @Override
    public MemberEntity queryOneById(Long id) {
        return dao.queryOneById(id);
    }

    @Override
    public R registr(MemberEntity entity) {
        // 1校验数据
        if(entity.getPhone() == null || entity.getPhone().equals("")
            ||entity.getPassWord() == null || entity.getPassWord().equals("")){
            return R.error("请输入手机号和密码");
        }
        Map params = new HashedMap();
        params.put("phone", entity.getPhone());
        params.put("status", 1);
        List list = this.listByCondition(params);
        if(list != null && list.size() > 0){
            return R.error("当前手机号已经注册");
        }
        // 2 保存
        entity.setUserId(UUID.randomUUID().toString().replaceAll("-", ""));
        this.insert(entity);
        return R.ok();
    }

    @Override
    public R login(MemberEntity entity) {
        // 1校验数据
        if(entity.getPhone() == null || entity.getPhone().equals("")
                ||entity.getPassWord() == null || entity.getPassWord().equals("")){
            return R.error("请输入手机号和密码");
        }
        Map params = new HashedMap();
        params.put("phone", entity.getPhone());
        params.put("passWord", entity.getPassWord());
        params.put("status", 1);
        List list = this.listByCondition(params);
        if(list == null || list.size() != 1){
            return R.error("手机号或密码错误");
        }else{
            return R.ok().put("userInfo", list.get(0));
        }

    }

}
